const router = require('express').Router();
const multer  = require('multer');
const path = require('path');


// 自定义文件存储路径以及文件名等
const storage = multer.diskStorage({
    // 存储文件目录
    destination: function (req, file, cb) {
      cb(null, './public/uploads')
    },
    // 自定义存储文件名
    filename: function (req, file, cb) {
      cb(null, file.fieldname + '-' + Date.now()+path.extname(file.originalname))
    }
})

const upload = multer({ storage: storage })
// 用于显示上传文件的页面的
router.get('/upload', (req, res) => {
    res.render('upload')
})
// 上传文件路由 
router.post('/upload', upload.single('file'), (req, res) => {
    // req.file 上传成功 文件信息  /static/uploads/
    console.log(req.file);
    res.send({
        code: 200,
        msg: '上传成功',
        data: {
            url: '/static/uploads/'+ req.file.filename
        }
    });
})
module.exports = router